package mi.payment.ui.base;

import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import mi.payment.R;
import mi.payment.db.dao.PayDetail;
import mi.payment.db.dao.UserInfo;
import mi.payment.transaction.AutoFinishActivity;
import sunmi.payment.L3.L3ErrorCode;
import sunmi.sunmiui.button.ButtonRoundCorner;
import sunmi.sunmiui.title.Title;

/**
 * 输入主管密码基类
 *
 * @author by lj on 2017/3/26.
 */
public class BaseInputAdminPwdActivity extends AutoFinishActivity {

    public static final String EXTRA_PARAMETER_PAY_DETAIL = "payment.extra.pay_detail";

    protected TextView cardNumber;
    protected TextView cardMoney;
    protected TextView cardTime;
    protected TextView cardProof;
    protected TextView cardRefer;
    protected TextView tvType;
    protected TextView ensureData;
    protected TextView transactionType;

    protected Title title;
    protected PasswordKeyboard passwordKeyboard;
    protected PasswordInputView passwordInputView;
    private ToneGenerator toneGenerator;
    protected ButtonRoundCorner okButton;

    protected PayDetail originPayDetail;
    protected boolean needInputAdminPwd;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adminstrator_cancellation_password);
        setTitle();
        initView();
        toneGenerator();
        setClickListener();
    }

    /**
     * 初始化界面
     */
    private void initView() {
        originPayDetail = (PayDetail) getIntent().getSerializableExtra(EXTRA_PARAMETER_PAY_DETAIL);

        cardNumber = (TextView) findViewById(R.id.card_id);
        cardMoney = (TextView) findViewById(R.id.card_money);
        cardTime = (TextView) findViewById(R.id.card_time);
        cardProof = (TextView) findViewById(R.id.card_proof);
        cardRefer = (TextView) findViewById(R.id.card_refer);
        transactionType = (TextView) findViewById(R.id.text_type);
        okButton = (ButtonRoundCorner) findViewById(R.id.btn_pay);
        okButton.setTitleText(getString(R.string.ok));
        okButton.setOnClickListener(this);

        int color = getResources().getColor(R.color.FD5A52);
        int color1 = getResources().getColor(R.color.CEC4D44);
        okButton.setBackgroundColor(color, color1);
        okButton.setWidth(592);
        okButton.setHeight(90);

        tvType = (TextView) findViewById(R.id.text_type);
        ensureData = (TextView) findViewById(R.id.ensure_data);
        passwordKeyboard = (PasswordKeyboard) findViewById(R.id.passwordKeyboard);
        passwordInputView = (PasswordInputView) findViewById(R.id.passwordInputView);
    }

    private void toneGenerator() {
        try {
            toneGenerator = new ToneGenerator(AudioManager.STREAM_SYSTEM, ToneGenerator.MAX_VOLUME);
        } catch (Exception e) {
            Log.e("BaseInputAdminPwdAdmin", "打开ToneGenerator失败");
            e.printStackTrace();
        }
    }

    private void setClickListener() {
        passwordKeyboard.setKeyClickCallback(new PasswordKeyboard.KeyClickCallback() {

            @Override
            public void onNumClick(int keyNum) {
                sound();
                passwordInputView.addText(String.valueOf(keyNum));
            }

            @Override
            public void onDelClick() {
                passwordInputView.delLast();
            }

            @Override
            public void onEnterClick() {
                clickListener();
            }

        });
    }

    /**
     * 提示音
     */
    private void sound() {
        if (toneGenerator != null) {
            toneGenerator.startTone(ToneGenerator.TONE_PROP_BEEP);
        }
    }

    /**
     * 判断主管密码是否正确
     */
    protected boolean isPasswordRight() {
        String password = passwordInputView.getPassword();
        UserInfo login = mUserInfoModel.login("00", password);
        return login != null && login.getUPwd().equals(passwordInputView.getPassword());
    }

    private void setTitle() {
        title = (Title) findViewById(R.id.input_password_title);
        title.setButtonVisibility(false, true, true, true);
        title.setHeadBgColor(getResources().getColor(R.color.colorBar));
        title.setMenuClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                handleL3Back(L3ErrorCode.ERROR_TRADE_CANCEL);
                finish();
            }

        }, null, null, null);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (toneGenerator != null) {
            toneGenerator.release();
        }
    }

    public void clickListener() {

    }


}
